<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<script type="text/javascript">
	$(function() {
		$("#div_enum").hide();
		$("#div_attr").hide();

		$("#btn_add_enum_item")
				.click(
						function() {
							//enum
							$("#type_flag").val("1");
							$("#enum_list")
									.append(
											'<div class="controls">'
													+ '<input type="hidden" name="enums" value="'
													+ $("#enum_item").val()
															.trim()
													+ '">'
													+ '<code>'
													+ $("#enum_item").val()
															.trim()
													+ '</code><button style="margin-left:50px;margin-bottom:5px" type="button" class="btn" onclick="deltr(this)">删除</button></div>');
							$("#div_enum").hide();
							$("#enum_item").val("");
						});

		$("#btn_add_attr_item")
				.click(
						function() {
							var length = $("#attr_length").val().trim();
							var code = $("#attr_type option:selected").text()
									+ "&nbsp;&nbsp;"
									+ $("#attr_item").val().trim();
							if (length == "" || length == "0") {
								code = code + ";";
								length = 0;
							} else {
								code = code + ":" + "&nbsp;&nbsp;" + length
										+ ";";
							}

							//struct
							$("#type_flag").val("2");
							$("#attr_list")
									.append(
											'<div class="controls">'
													+ '<input type="hidden" name="attrs" value="'
													+ $("#attr_item").val()
															.trim()
													+ '">'
													+ '<input type="hidden" name="attr_length" value="'+length+'">'
													+ '<input type="hidden" name="types" value="'
													+ $(
															"#attr_type option:selected")
															.val()
													+ '">'
													+ '<code>'
													+ code
													+ '</code><button style="margin-left:50px;margin-bottom:5px" type="button" class="btn" onclick="deltr(this)">删除</button></div>');
							$("#div_attr").hide();
							$("#attr_item").val("");
							$("#attr_length").val("");
						});
	});
	function deltr(delbtn) {
		$(delbtn).parents(".controls").remove();
	}
	function hide_show_enum() {
		$("#div_enum").toggle("normal");
	}

	function hide_show_attr() {
		$("#div_attr").toggle("normal");
	}
	function valid() {
		$("#help_name").val("英文");
		$("#div_name").removeClass("control-group error");
		$("#div_name").addClass("control-group");
		var name = $("#name").val().trim();
		if (name == "") {
			$("#div_name").removeClass("control-group");
			$("#div_name").addClass("control-group error");
			document.getElementById("help_name").innerHTML = "英文,类型实体名称不能为空";
			return false;
		}
		var type_flag = $("#type_flag").val();
		var enums = $("#enum_list").children().length;
		var items = $("#attr_list").children().length;
		if (type_flag == 1 && enums == 0) {
			alert("请为该类型添加枚举项或者结构体属性！");
			return false;
		} else if (type_flag == 2 && items == 0) {
			alert("请为该类型添加枚举项或者结构体属性！");
			return false;
		}

		return true;
	}
	
	function goback() {
		history.go(-1);
	}
</script>

<div class="page-header">
	<h3 style="margin-top: auto">新增类型实体</h3>
</div>
<div class="row-fluid">
	<div>
		<form class="form-horizontal"
			action="<%=request.getContextPath()%>/domain_entity/type/add"
			method="POST" onsubmit="return valid()">
			<div class="control-group" id="div_name">
				<label class="control-label">名称</label>
				<div class="controls">
					<input type="text" placeholder="名称" id="name" name="name">
					<span class="help-inline" id="help_name">英文</span>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label">中文名称</label>
				<div class="controls">
					<input type="text" placeholder="中文名称" id="cnName" name="cnName">
				</div>
			</div>
			<div class="control-group">
				<label class="control-label">描述信息</label>
				<div class="controls">
					<textarea rows="3" class="input-xxlarge" placeholder="描述信息"
						id="description" name="description"></textarea>
				</div>
			</div>


			<div class="tabbable">
				<!-- Only required for left/right tabs -->
				<ul class="nav nav-tabs">
					<li class="active"><a href="#tab1" data-toggle="tab">枚举类型</a></li>
					<li><a href="#tab2" data-toggle="tab">结构体类型</a></li>
				</ul>
				<div class="tab-content">
					<div class="tab-pane active" id="tab1">
						<div class="control-group">
							<label class="control-label">枚举项</label>
							<div id="enum_list"></div>
							<div class="controls">
								<button type="button" class="btn" id="btn_add_enum"
									onclick="hide_show_enum()"><i class="icon-plus"></i>&nbsp;添加枚举项</button>
							</div>
						</div>
						<div class="control-group" id="div_enum">
							<div class="controls">
								<div class="form-inline">
									<input type="text" class="input-medium" placeholder="枚举项"
										id="enum_item">
									<button type="button" class="btn" id="btn_add_enum_item"><i class="icon-plus"></i>&nbsp;添加</button>
								</div>
							</div>
						</div>
					</div>
					<div class="tab-pane" id="tab2">
						<div class="control-group">
							<label class="control-label">属性列表</label>
							<div id="attr_list"></div>
							<div class="controls">
								<button type="button" class="btn" id="btn_add_attr"
									onclick="hide_show_attr()"><i class="icon-plus"></i>&nbsp;添加属性</button>
							</div>
						</div>

						<div class="control-group" id="div_attr">
							<div class="controls">
								<div class="form-inline">
									属性类型&nbsp;&nbsp;<select style="width: 200px" id="attr_type">
										<c:choose>
											<c:when test="${!empty typeList}">
												<c:forEach items="${typeList}" var="item">
													<option value="${item.id}">
														<c:out value="${item.name}" />
													</option>
												</c:forEach>

											</c:when>
										</c:choose>
									</select> <input type="text" class="input-medium" placeholder="属性名称"
										id="attr_item"> <input type="text"
										class="input-medium" placeholder="所占位数" id="attr_length">
									<button type="button" class="btn" id="btn_add_attr_item"><i class="icon-plus"></i>&nbsp;添加</button>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>



			<div class="control-group">
				<div class="controls">
					<input type="hidden" id="type_flag" name="type_flag" value="1">
					<button type="submit" class="btn" id="submit" ><i class="icon-ok"></i>&nbsp;提交</button>
					<button type="button" class="btn" id=""><i class="icon-remove"></i>&nbsp;取消</button>
				</div>
			</div>
		</form>
	</div>
</div>
<!--/row-->